/*
 * 版权所有 ( c ) 联想集团有限公司 2005-2009 保留所有权利。
 * 项目：	Lenovo Push Service
 * 文件名：	DateUtil.java
 * 版本:    1.0
 * 描述：	
 * 作者：	刘兵召
 * 日期：	Apr 14, 2010 
 * 修改历史：
 * 【时间】		【修改者】	【修改内容】
 */
package com.feng.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 日期工具类
 * 
 * @author MaChao
 * 
 */
public class DateUtil {

	private static GregorianCalendar gc = null;
	public static final String YYYY_MM_DD_HHMMSS = "yyyy-MM-dd HH:mm ss";

	static {
		gc = new GregorianCalendar(Locale.CHINA);
		gc.setLenient(true);
		gc.setFirstDayOfWeek(Calendar.MONDAY);
	}

	private static void initCalendar(Date date) {
		if (date == null) {
			throw new IllegalArgumentException("argument date must be not null");
		}
		gc.clear();
		gc.setTime(date);
	}

	/**
	 * 返回传入日期的后一天
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 * @author MaChao
	 */
	public static Date getNextDay(Date date) {
		initCalendar(date);
		long tomorrow = gc.getTimeInMillis() + 1 * 60 * 60 * 24 * 1000;
		gc.setTimeInMillis(tomorrow);
		return gc.getTime();
	}

	/**
	 * 返回传入日期的前一天
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 * @author MaChao
	 */
	public static Date getLastDay(Date date) {
		initCalendar(date);
		long yestarday = gc.getTimeInMillis() - 1 * 60 * 60 * 24 * 1000;
		gc.setTimeInMillis(yestarday);
		return gc.getTime();
	}

	/**
	 * 获得日期所在月的最后一天
	 * 
	 * @param String
	 *            dateStr(yyyy-MM-dd)
	 * @return String(yyyy-MM-dd)
	 * @throws ParseException
	 * @author MaChao
	 */
	public static String getLastMonthDay(String dateStr) throws ParseException {
		Date date = praseString2Date(dateStr);
		Date lastMonthDay = getLastMonthDay(date);
		return formatDate2ShortString(lastMonthDay);
	}

	/**
	 * 将Date格式化为短型String
	 * 
	 * @param Date
	 *            date
	 * @return String(yyyy-MM-dd)
	 * @author MaChao
	 */
	public static String formatDate2ShortString(Date date) {
		//initCalendar(date);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = sdf.format(date);
		return dateStr;
	}
	
	/**
	 * 将Date格式化为短型String
	 * 
	 * @param Date
	 *            date
	 * @return String(yyyy-MM-dd)
	 * @author MaChao
	 */
	public static String formatDate2String(Date date,String foratSimple) {
		//initCalendar(date);
		SimpleDateFormat sdf = new SimpleDateFormat(foratSimple);
		String dateStr = sdf.format(date);
		return dateStr;
	}
	
	/**
	 * 将短日期格式的String转化为Date类型
	 * 
	 * @param String
	 *            dateStr(yyyy-MM-dd)
	 * @return Date
	 * @throws ParseException
	 * @author MaChao
	 */
	public static Date praseString2Date(String dateStr) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = (Date) sdf.parse(dateStr);
		return date;
	}

	/**
	 * 将长日期格式的String转化为Date类型
	 * 
	 * @param String
	 *            dateStr(yyyy-MM-dd HH:mm:ss)
	 * @return Date
	 * @throws ParseException
	 * @author MaChao
	 */
	public static Date praseString2LongDate(String dateStr)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = (Date) sdf.parse(dateStr);
		return date;
	}

	/**
	 * 取得日期所在周的第一天
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 */
	public static Date getFirstWeekDay(Date date) {
		initCalendar(date);
		gc.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return gc.getTime();
	}

	/**
	 * 取得日期所在周的最后一天
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 */
	public static Date getLastWeekDay(Date date) {
		initCalendar(date);
		gc.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return gc.getTime();
	}

	/**
	 * 取得日期所在月的第一天
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 */
	public static Date getFirstMonthDay(Date date) {
		initCalendar(date);
		int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
		gc.add(Calendar.DAY_OF_MONTH, 1 - dayOfMonth);
		return gc.getTime();
	}

	/**
	 * 取得日期所在月的最后一天
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 */
	public static Date getLastMonthDay(Date date) {
		initCalendar(date);
		int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
		int maxDaysOfMonth = gc.getActualMaximum(Calendar.DAY_OF_MONTH);
		gc.add(Calendar.DAY_OF_MONTH, maxDaysOfMonth - dayOfMonth);
		return gc.getTime();
	}

	/**
	 * 取得日期所在旬的最后一天
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 */
	public static Date getFirstTenDaysDay(Date date) {
		initCalendar(date);
		int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
		if (dayOfMonth <= 10) {
			gc.set(Calendar.DAY_OF_MONTH, 1);
		} else if (dayOfMonth > 20) {
			gc.set(Calendar.DAY_OF_MONTH, 21);
		} else {
			gc.set(Calendar.DAY_OF_MONTH, 11);
		}
		return gc.getTime();
	}

	/**
	 * 取得日期所在旬的最后一天
	 * 
	 * @param Date
	 *            date
	 * @return Date
	 */
	public static Date getLastTenDaysDay(Date date) {
		initCalendar(date);
		int dayOfMonth = gc.get(Calendar.DAY_OF_MONTH);
		if (dayOfMonth <= 10) {
			gc.set(Calendar.DAY_OF_MONTH, 10);
		} else if (dayOfMonth > 20) {
			gc.set(Calendar.DAY_OF_MONTH, gc
					.getActualMaximum(Calendar.DAY_OF_MONTH));
		} else {
			gc.set(Calendar.DAY_OF_MONTH, 19);
		}
		return gc.getTime();
	}

	public static void main(String[] args) {
		Date date;
		try {
			date = praseString2Date("2010-04-16");
			System.out.println("Today = " + formatDate2ShortString(date));
			System.out.println("Yesterday = " + formatDate2ShortString(getLastDay(date)));
			System.out.println("Tomorrow = " + formatDate2ShortString(getNextDay(date)));
			System.out.println("getFirstWeekDay = " + getFirstWeekDay(date));
			System.out.println("getLastWeekDay = " + getLastWeekDay(date));
			System.out.println("getFirstMonthDay = " + getFirstMonthDay(date));
			System.out.println("getLastMonthDay = " + getLastMonthDay(date));
			System.out.println("getFirstTenDaysDay = " + getFirstTenDaysDay(date));
			System.out.println("getLastTenDaysDay = " + getLastTenDaysDay(date));
			System.out.println("getLastMonthDay = " + getLastMonthDay("2010-04-16"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}
